﻿/*
Copyright (c) 2008 Ryan BB Schreiber, Brendan G Fry

Permission is hereby granted, free of charge, to any person obtaining a copy of 
this software and associated documentation files (the "Software"), to deal in 
the Software without restriction, including without limitation the rights to 
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
of the Software, and to permit persons to whom the Software is furnished to do 
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/
using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;

namespace Darkside.GeneratorStudio.UI
{
    public partial class SQLConnectionDialog : Form
    {
        private readonly SqlFileConnectionProperties _sqlFileConnectionProperties;

        public SQLConnectionDialog()
        {
            InitializeComponent();
            _sqlFileConnectionProperties = new SqlFileConnectionProperties();
            _sqlFileConnectionProperties["Timeout"] = 10;
            
            // We don't want user instance to be set.
            _sqlFileConnectionProperties["User Instance"] = false;
            sqlConnectionUIControl.Initialize(_sqlFileConnectionProperties);
        }


        private bool _advancedShown;
        private void btnAdvanced_Click(object sender, EventArgs e)
        {
            pgAdvanced.SelectedObject = _sqlFileConnectionProperties;
            if (!_advancedShown)
            {
                OpenAdvanced();
            }
            else
            {
                CloseAdvanced();
            }
        }

        public void CloseAdvanced()
        {
            SuspendLayout();
            _advancedShown = false;
            Width = 408;
            pgAdvanced.Visible = false;
            Left += 259;
            ResumeLayout(true);
        }

        public void OpenAdvanced ()
        {
            SuspendLayout();
            _advancedShown = true;
            pgAdvanced.Visible = true;
            Width = 667;
            Left -= 259;
            ResumeLayout(true);
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            var conn = new SqlConnection
                           {
                               ConnectionString = _sqlFileConnectionProperties.ConnectionStringBuilder.ConnectionString
                           };
            try
            {
                conn.Open();
                MessageBox.Show("Connection test succeeded.", "Darkside Generator Studio", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception)
            {
                MessageBox.Show("Connection test failed.", "Darkside Generator Studio", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                conn.Close();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool testCompleted = false;
            var conn = new SqlConnection
            {
                ConnectionString = _sqlFileConnectionProperties.ConnectionStringBuilder.ConnectionString
            };
            try
            {
                conn.Open();
                testCompleted = true;
            }
            catch (Exception)
            {
                MessageBox.Show("Connection test failed. Please fix connections settings to continue.", "Darkside Generator Studio", MessageBoxButtons.OK, MessageBoxIcon.Error);
                testCompleted = false;
            }
            finally
            {
                conn.Close();
            }

            if (testCompleted)
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void SQLConnectionDialog_Load(object sender, EventArgs e)
        {
            CloseAdvanced();
            sqlConnectionUIControl.LoadProperties();
        }

        public String ConnectionString
        {
            get
            {
                return _sqlFileConnectionProperties.ConnectionStringBuilder.ConnectionString;
            }
            set
            {
                _sqlFileConnectionProperties.ConnectionStringBuilder.ConnectionString = value;
            }
        }
    }
}
